<script>
import ZIM from '@/js_sdk/zego-ZIMUniplugin-JS/lib/ZIM.js';
import { getMessage_count } from '@/api/exchange.js';

export default {
	methods: {
		init() {
			let token = uni.getStorageSync('token');
			// #ifdef APP-PLUS
			if (token) {
				this.$zegoZim.zegoLogin();
				this.getMessages();
				this.goEasy();
			}
			this.zimInit();
			//	#endif
		},

		// 获取状态栏高度
		getStatusBarHeight() {
			uni.getSystemInfo({
				success: (e) => {
					let statusBarHeight = 0;
					// #ifdef APP-PLUS
					statusBarHeight = e.statusBarHeight;
					// #endif

					// #ifdef H5
					statusBarHeight = 0;
					// #endif
					this.$store.commit('SET_STATUS_BAR', statusBarHeight);
				}
			});
		},
		//获取软件的版本号
		getVersion() {
			let version = uni.getSystemInfoSync().appVersion;
			this.$store.commit('SET_VERSION', version);
			uni.setStorageSync('version', version);
		},
		//即购聊天初始化
		zimInit() {
			console.log('即购聊天初始化');
			// 静态同步方法，创建 zim 实例，传入 AppID 和 AppSign
			// create 方法仅第一次调用时会创建 ZIM 实例，后续调用会返回 null。
			ZIM.create({
				appID: 1472263955,
				appSign: '0d8c26d495532eed3a1e56f4b2e3520e177cf176f0930e0fa3157104c43deaa0'
			});
		},

		goEasy() {
			let that = this;
			//建立连接
			this.$goEasy.connect({
				onSuccess: function () {
					that.subscribeChannel(); //去订阅
				},
				onFailed: function (error) {
					//连接失败
					console.log('GoEasy 连接失败, code:' + error.code + ',error:' + error.content);
				}
			});
		},
		//goEasy订阅
		subscribeChannel() {
			let userInfo = uni.getStorageSync('userInfo');
			console.log('订阅通道', userInfo);
			let that = this;
			that.$goEasy.pubsub.subscribe({
				channel: userInfo.id, //替换为您自己的channel
				onMessage: function (message) {
					console.log('webSocketMessage 注入vuex', message);
					//提交更新到vuex中
					that.$store.commit('webSocketMessage', message);

					//收到消息
					console.log('Channel:' + message.channel + ' content:' + message.content);
					if (message.content == 1) {
						//消息数量
						setTimeout(() => {
							that.getMessages();
						}, 800);
					}
				},
				onSuccess: function () {
					console.log('Channel订阅成功。');
				},
				onFailed: function (error) {
					console.log('Channel订阅失败, 错误编码：' + error.code + ' 错误信息：' + error.content);
				}
			});
		},


		getMessages() {
			getMessage_count({}).then(res => {
				if (res.code == 1) {
					this.$store.state.inviteValue = res.data.dengnilaida;
				}
			})
		},
	},
	onLaunch() {
		this.getStatusBarHeight();
		this.getVersion();
		this.init();
	},
	onShow() {
		// #ifdef APP-PLUS
		if (uni.getStorageSync('token')) {
			this.getMessages();
		}
		// #endif
	},
	onHide() { }
};
</script>
<style lang="scss">
/* 注意要写在第一行，同时给style标签加入lang="scss"属性 */
@import 'uview-ui/index.scss';
@import '@/uni_modules/uni-scss/index.scss';
/*每个页面公共css */
@import './static/css/public.css';
// @import './static/css/reset.css';

body {
	margin: 0;
	font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', 'Microsoft Yahei',
		sans-serif;
	-webkit-font-smoothing: antialiased;
	-moz-osx-font-smoothing: grayscale;

}

::-webkit-scrollbar {
	display: none;
}

view{
	box-sizing: border-box;
}

/deep/ .uni-scroll-view-refresh__spinner>circle {
	color: #2979ff !important;
}

/deep/ .uni-scroll-view-refresh-inner>svg {
	fill: #2979ff !important;
}
</style>